package com.example.lib_cache.viewmodel;

import android.content.Context;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.lib_cache.model.Order;
import com.example.lib_cache.network.exception.ApiException;
import com.example.lib_cache.network.exception.ErrorConsumer;
import com.example.lib_cache.network.response.ResponseResultTransformer;
import com.example.lib_cache.service.ApiService;

import io.reactivex.functions.Consumer;

public class WaitingActivityViewModel extends ViewModel {

    private final static String TAG = WaitingActivityViewModel.class.getSimpleName();

    private MutableLiveData<Order> orderMutableLiveData = new MutableLiveData<>();

    private MutableLiveData<Boolean> timerExit = new MutableLiveData<>();

    public MutableLiveData<Order> getOrderMutableLiveData() {
        return orderMutableLiveData;
    }

    public void setOrderMutableLiveData(Order order) {
        this.orderMutableLiveData.postValue(order);
    }

    public MutableLiveData<Boolean> getTimerExit() {
        return timerExit;
    }

    public void setTimerExit(boolean result) {
        this.timerExit.postValue(result);
    }

    public void initData(){
    }

    public void startWaiting(ApiService apiService, Context context){
        apiService.getSendingOrderStatus()
                .compose(ResponseResultTransformer.<Order>obtain())
                .subscribe(new Consumer<Order>() {
                    @Override
                    public void accept(Order order) throws Exception {
                        Log.d(TAG,"获取成功");
                        if (order.getOrderStatus()==1){

                            setTimerExit(true);
                        }
                    }
                }, new ErrorConsumer() {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"获取失败"+ex.getMessage());
                        Log.d(TAG,"ErrorMsg:"+ex.getErrorMsg());
                    }
                });
    }
}
